package com.wanshi.service;

import com.wanshi.dao.UserDao;
import com.wanshi.entity.PageInfo;
import com.wanshi.entity.User;

import java.util.List;

public class UserService {

    private UserDao userDao = new UserDao();

    public User findUserByNameAndPsd(String username, String password) {
        return userDao.findUserByNameAndPsd(username,password);
    }

    public User findUserByName(String username) {
        return userDao.findUserByName(username);
    }

    public void addUser(User user) {
        userDao.addUser(user);
    }

    public List<User> list() {
        return userDao.list();
    }

    public int delUser(String id) {

        return userDao.delUser(id);
    }

    public User findById(String id) {
        return userDao.findById(id);
    }

    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    public void deleteBatch(String[] ids) {
        userDao.deleteBatch(ids);
    }

    public List<User> findByCond(User user) {
        return userDao.findByCond(user);
    }

    //3  5   select * from user limit 10,5
    public PageInfo findByPage(int page, int size) {
        PageInfo pageInfo = new PageInfo();
        //list  total  page  size

        pageInfo.setPage(page);
        pageInfo.setSize(size);

        List<User> list = userDao.findByPage( (page-1)*size,size);
        pageInfo.setList(list);

        Long total = userDao.getCount();
        pageInfo.setTotal(total);


        return pageInfo;
    }

    public PageInfo findByPageAndCond(int page, int size ,User user) {
        PageInfo pageInfo = new PageInfo();

        pageInfo.setSize(size);

        Long total =userDao.getCondByCound(user);
        pageInfo.setTotal(total);

        if (page>pageInfo.getLast()){
            page = pageInfo.getLast();
        }
        if (page < 1){
            page = 1;
        }
        pageInfo.setPage(page);
        List<User> list= userDao.findByPageAndCond((page-1)*size,size,user);
        pageInfo.setList(list);
        return pageInfo;
    }
}
